package com.project.controller.web;

import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.entity.Class;
import com.project.myBeans.CourseTeacherDetail;
import com.project.myBeans.R;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@EnableAutoConfiguration
@RestController
@Api(description = "教师所授课程与学生关联接口", value = "CourseStudent")
@RequestMapping("/coursestudent")
public class CourseStudentController{

    @Autowired
    CourseStudentService courseStudentService;

    @Autowired
    CourseTeachService courseTeachService;

    @Autowired
    CourseService courseService;

    @Autowired
    UserService userService;

    @Autowired
    StudentInfoService studentInfoService;

    @Autowired
    TeacherInfoService teacherInfoService;

    @Autowired
    ClassService classService;
    /**
     * wwh 2018/04/01
     * @param studentInfoDetail
     * @param courseteacherid
     * @return
     */
    @ApiOperation("单个加入学生名单")
    @PostMapping("/addOne/{courseteacherid}")
    public R addOne(@RequestBody StudentInfoDetail studentInfoDetail, @PathVariable Integer courseteacherid){
        try{
            User user = studentInfoDetail.getUser();    //前端数据
            StudentInfo studentInfo = studentInfoDetail.getStudentInfo();   //前端数据

            User user1 = userService.findByWorkId(user.getWorkid());    //通过用户名(学号查找数据库中是否已存在)
            if(user1!=null){    //已存在该学生的记录
                StudentInfo studentInfo1 = studentInfoService.findByUserId(user1.getId());
                if(studentInfo.getClassid().equals(studentInfo1.getClassid())){ //班级信息相符合
                    CourseStudent courseStudent = new CourseStudent();
                    courseStudent.setCourseteacherid(courseteacherid);
                    courseStudent.setStudentid(studentInfo1.getId());
                    courseStudentService.add(courseStudent);
                }else {
                    return R.error("该学生信息不符");
                }
            }else { //尚未存在该学生记录
                Class clazz = classService.findById(studentInfo.getClassid());

                user.setSchoolid(clazz.getSchoolid());
                user.setUniversityid(clazz.getUniversityid());
                user.setUsertype(2);
                user.setState((short)2);
                userService.add(user);

                studentInfo.setUserid(user.getId());
                studentInfo.setMajorid(clazz.getMajorid());
                studentInfo.setDepartmentid(clazz.getDepartmentid());
                studentInfo.setSchoolid(clazz.getSchoolid());
                studentInfoService.add(studentInfo);
                CourseStudent courseStudent = new CourseStudent();
                courseStudent.setCourseteacherid(courseteacherid);
                courseStudent.setStudentid(studentInfo.getId());
                courseStudentService.add(courseStudent);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh  2018/04/03
     * @param courseStudent
     * @return
     */
    @ApiOperation("按照课程教师id及关键字条件查找学生")
    @PostMapping("/findStudentsByCondition")
    public R findStudentsByCondition(@RequestBody CourseStudent courseStudent){
        try {
            List<CourseStudent> courseStudentList = courseStudentService.findByCourseTeacherid(courseStudent.getCourseteacherid());
            List<StudentInfoDetail> studentInfoDetailList = new ArrayList<>();
            for(CourseStudent courseStudent1:courseStudentList){
                StudentInfoDetail studentInfoDetail = studentInfoService.findDetailById((courseStudent1.getStudentid()));
                studentInfoDetailList.add(studentInfoDetail);
            }
            if(courseStudent.getKeyword()!=null) {
                List<StudentInfoDetail> studentInfoDetails = studentInfoDetailList.stream().filter((x) -> {
                    return x.getClazz().getName().contains(courseStudent.getKeyword())
                            || x.getUser().getWorkid().contains(courseStudent.getKeyword())
                            || x.getUser().getRealname().contains(courseStudent.getKeyword());
                }).collect(Collectors.toList());
                return R.ok().put("data", studentInfoDetails).put("total", MyPageHelper.getTotal(studentInfoDetails));
            }
            return R.ok().put("data",studentInfoDetailList).put("total",MyPageHelper.getTotal(studentInfoDetailList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    /**
     * wwh 2018/04/03
     * @param courseStudent
     * @return
     */
    @ApiOperation("按照学生id查找课程及关键字搜索")
    @PostMapping("/findCoursesByCondition")
    public R findCoursesByCondition(@RequestBody CourseStudent courseStudent){
        try{
            List<CourseStudent> courseStudentList = courseStudentService.findByStudentid(courseStudent.getStudentid());
            List<CourseTeacherDetail> courseTeacherDetailList = new ArrayList<>();
            for(CourseStudent courseStudent1:courseStudentList){
                CourseTeach courseTeach = courseTeachService.findById(courseStudent1.getCourseteacherid());
                CourseTeacherDetail courseTeacherDetail = new CourseTeacherDetail();
                courseTeacherDetail.setCourse(courseService.findById(courseTeach.getCourseid()));
                courseTeacherDetail.setCourseTeach(courseTeach);
                courseTeacherDetail.setTeacherInfoDetail(teacherInfoService.findDetailByTeacherId(courseService.findById(courseTeach.getCourseid()).getCreatorid()));
                courseTeacherDetail.setTeachDetail(teacherInfoService.findDetailByTeacherId(courseTeach.getTeacherid()));
                courseTeacherDetailList.add(courseTeacherDetail);
            }
            if(courseStudent.getKeyword()!=null){
                List<CourseTeacherDetail> courseTeacherDetails = courseTeacherDetailList.stream().filter(x->{
                    return x.getCourse().getName().contains(courseStudent.getKeyword())
                            || x.getTeachDetail().getUser().getRealname().contains(courseStudent.getKeyword())
                            || x.getTeacherInfoDetail().getUser().getRealname().contains(courseStudent.getKeyword());
                }).collect(Collectors.toList());
                return R.ok().put("data",courseTeacherDetails).put("total",MyPageHelper.getTotal(courseTeacherDetails));
            }
            return R.ok().put("data",courseTeacherDetailList).put("total",MyPageHelper.getTotal(courseTeacherDetailList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Wqd 2018/4/2
     * @param studentid
     * @return
     */
    @ApiOperation("通过id删除加入课程的学生")
    @GetMapping("/remove/{studentid}")
    public R removeOne(@PathVariable Integer studentid){
        courseStudentService.remove(studentid);
        return R.ok();
    }
}
